package com.ruoyi.publish.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.publish.domain.BestJenkinsBuilds;
import com.ruoyi.publish.service.IBestJenkinsBuildsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 获取新包Controller
 * 
 * @author ruoyi
 * @date 2025-03-10
 */
@RestController
@RequestMapping("/publish/builds")
public class BestJenkinsBuildsController extends BaseController {
    @Autowired
    private IBestJenkinsBuildsService bestJenkinsBuildsService;



    /**
     * 查询获取新包列表
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:list')")
    @GetMapping("/list")
    public TableDataInfo list(BestJenkinsBuilds bestJenkinsBuilds) {
        startPage();
        List<BestJenkinsBuilds> list = bestJenkinsBuildsService.selectBestJenkinsBuildsList(bestJenkinsBuilds);
        return getDataTable(list);
    }

    /**
     * 导出获取新包列表
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:export')")
    @Log(title = "获取新包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BestJenkinsBuilds bestJenkinsBuilds) {
        List<BestJenkinsBuilds> list = bestJenkinsBuildsService.selectBestJenkinsBuildsList(bestJenkinsBuilds);
        ExcelUtil<BestJenkinsBuilds> util = new ExcelUtil<BestJenkinsBuilds>(BestJenkinsBuilds.class);
        util.exportExcel(response, list, "获取新包数据");
    }

    /**
     * 获取获取新包详细信息
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bestJenkinsBuildsService.selectBestJenkinsBuildsById(id));
    }

    /**
     * 新增获取新包
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:add')")
    @Log(title = "获取新包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BestJenkinsBuilds bestJenkinsBuilds) {
        return toAjax(bestJenkinsBuildsService.insertBestJenkinsBuilds(bestJenkinsBuilds));
    }

    /**
     * 修改获取新包
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:edit')")
    @Log(title = "获取新包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BestJenkinsBuilds bestJenkinsBuilds) {
        return toAjax(bestJenkinsBuildsService.updateBestJenkinsBuilds(bestJenkinsBuilds));
    }

    /**
     * 删除获取新包
     */
    @PreAuthorize("@ss.hasPermi('publish:builds:remove')")
    @Log(title = "获取新包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bestJenkinsBuildsService.deleteBestJenkinsBuildsByIds(ids));
    }


    @Anonymous
    @GetMapping("/download/jar/{id}")
    public ResponseEntity<FileSystemResource> download(@PathVariable("id") Long id) throws FileNotFoundException {
        // 创建文件对象
        //String filePath = "C:/Users/Administrator/user-server.jar";
        BestJenkinsBuilds bestJenkinsBuilds = bestJenkinsBuildsService.selectBestJenkinsBuildsById(id);
        String filePath = bestJenkinsBuilds.getFilePath();
        // 检查文件是否存在
        File file = new File(filePath);
        if (!file.exists()) {
            return ResponseEntity.notFound().build();
        }
        // 设置响应头，以便浏览器下载文件
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        FileSystemResource resource = new FileSystemResource(file);
        return ResponseEntity.ok()
                .headers(headers)
                .body(resource);

    }

}
